<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\30 0030
 * Time: 10:19
 */

namespace app\Lib\Model\Img;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\User\UserHandle;

class Img implements \app\Lib\InterfaceIn\Img\Img
{
    private  $imgId;
    private  $cache;
    private  $cache_prefix = 'img_cache_' ;
    private  $cacheTime ='604800';
    private  $Db;
    private  $filed;
    public function __construct($imgId, $cache = true)
    {
        $this->cache = $cache;
        $this->imgId = $imgId;
        $this->cache_prefix = 'img_cache_'.$imgId.'_';
    }

    public function getImgId()
    {
        return $this->imgId ;
    }

    /**
     * 获取上传用户类
     * @return UserHandle
     */
    public function getUidClass()
    {
        // TODO: Implement getUidClass() method.
    }

    /**
     * 获取文件大小
     * @return string
     */
    public function getFileSize()
    {
        // TODO: Implement getFile_size() method.
    }

    /**
     * 获取上传时间
     * @return int
     */
    public function getCreateTime()
    {
        // TODO: Implement getCreateTime() method.
    }

    /**
     * 获取中文状态
     * @return string
     */
    public function getStatusStr()
    {
        // TODO: Implement getStatusStr() method.
    }

    /**
     * 获取下载数量
     * @return string
     */
    public function getDownloadNum()
    {
        // TODO: Implement getDownloadNum() method.
    }

    /**
     * 获取文件的唯一码
     * @return string
     */
    public function getFileKey()
    {
        // TODO: Implement getFileKey() method.
    }

    /**
     * 获取文件的名称
     * @return string
     */
    public function getFilename()
    {
        $this->filed = $this->getDb()->_filename;
        return  $this->getFiledValue();
    }

    /**
     * 获取文件的路径
     * @return string
     */
    public function getFilePath()
    {
        $this->filed = $this->getDb()->_file_path;
        return  $this->getFiledValue();
    }

    /**
     * 获取文件的MD5值
     * @return string
     */
    public function getFileMd5()
    {
        // TODO: Implement getFileMd5() method.
    }

    /**
     * 获取文件的sha1值
     * @return string
     */
    public function getFileSha1()
    {
        // TODO: Implement getFile_sha1() method.
    }

    /**
     * 获取文件后缀
     * @return string
     */
    public function getSuffix()
    {
        // TODO: Implement getSuffix() method.
    }

    public function getMore()
    {
        // TODO: Implement getMore() method.
    }

    private function getFiledValue(){
        if($this->cache){
            $result = $this->getCache();
        }else{
            $result = $this->getDbFiledValue();
        }
        return $result;
    }


    private function getCache(){
        $cacheName = $this->getCacheName();
        $cacheData = cache($cacheName);
        if(!$cacheData){
            $cacheData = $this->getDbFiledValue();
            cache($cacheName,$cacheData,$this->cacheTime);
        }
        return $cacheData;
    }

    private function getDbFiledValue(){
        $model = $this->getDb();
        $result = $model->where([$model->_id=>$this->imgId])->value($this->filed);
        return empty($result)?'无':$result;
    }

    private function getDb(){
        if(empty($this->Db)){
            $this->Db = new \app\Lib\Model\DbModel\Img();
        }
        return $this->Db;
    }

    private function getCacheName(){
        return $this->cache_prefix.'_'.$this->filed;
    }

    /**
     * 删除类的缓存
     * @return bool
     * @throws BlException
     */
    public function delAllCache()
    {
        $model = $this->getDb();
        $result =  $model->getTableFields();
        foreach ($result as $val){
            $this->delSingleCache($val);
        }
        return true;
    }

    /**
     * 删除单个字段缓存
     * @param $field
     * @return bool
     * @throws BlException
     */
    public function delSingleCache($field)
    {
        $this->filed = $field;
        if(empty($field)){
            throw new BlException('请提供要清除缓存的字段');
        }
        $cacheName = $this->getCacheName();
        cache($cacheName,null);
        return true;
    }

}